package com.gitee.minimalismstyle.fresh.common.document.util

import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.apache.poi.ss.usermodel.Workbook
import org.apache.poi.xssf.usermodel.XSSFWorkbook
import org.springframework.web.multipart.MultipartFile
import java.io.File

object ExeclUtils {
    fun colStrToNum(column: String ): Int{
        var num = 0
        var result = 0
        val length = column.length
        for (i in 0 until length) {
            val ch: Char = column.get(length - i - 1)
            num = (ch - 'A' + 1)
            num *= Math.pow(26.0, i.toDouble()).toInt()
            result += num
        }
        return result - 1
    }

    fun colIndexToStr(columnIndex: Int): String? {
        var columnIndex = columnIndex
        if (columnIndex <= 0) {
            return null
        }
        var columnStr = ""
        columnIndex--
        do {
            if (columnStr.length > 0) {
                columnIndex--
            }
            columnStr = (columnIndex % 26 + 'A'.toInt()).toChar().toString() + columnStr
            columnIndex = ((columnIndex - columnIndex % 26) / 26)
        } while (columnIndex > 0)
        return columnStr
    }

    fun getWorkbook(file: File): Workbook {
        val workbook : Workbook
        if(file.name.endsWith(".xls", true)){
            workbook = HSSFWorkbook(file.inputStream())
        }else{
            workbook = XSSFWorkbook(file.inputStream())
        }
        return workbook
    }
}